package com.polaris.live.ui.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.viewModelScope
import com.bumptech.glide.Glide
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.DateUtils
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.WidgetCallPromotionBinding
import com.polaris.live.common.manager.startCountdown
import com.polaris.live.resp.back_resp.CallPromotionResp
import com.polaris.live.utils.image.loadImage
import kotlinx.coroutines.Job

/**
 * CallPromotionView
 *
 * @author Created by 半仙 on 2023/11/16/016 9:25
 */
class CallPromotionView(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {

    private val mBinding =
        WidgetCallPromotionBinding.inflate(LayoutInflater.from(context), this, true)

    private var isAddObserver = false

    private var job: Job? = null

    private var onSetClickListener: ((call: CallPromotionResp) -> Unit)? = null
    fun onSetClickListener(click: ((call: CallPromotionResp) -> Unit)) {
        onSetClickListener = click
    }

    private var onTimeFinishListener: (() -> Unit)? = null
    fun setOnTimeFinishListener(l: () -> Unit) {
        onTimeFinishListener = l
    }

    init {
        hideView()
    }

    fun hideView() {
        visibility = GONE
        ViewUtils.setViewVisible(this, GONE)
    }

    fun setData(
        callPromotionResps: List<CallPromotionResp>,
        viewLifecycleOwner: LifecycleOwner,
        mViewModel: BaseViewModel,
    ) {
        //只添加一次监听就够了
        if (!isAddObserver) {
            viewLifecycleOwner.lifecycle.addObserver(object : LifecycleEventObserver {
                override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                    isAddObserver = true
                    if (event == Lifecycle.Event.ON_DESTROY) {
                        job?.cancel()
                        job = null
                    }
                }
            })
        }
        val payResp = callPromotionResps.getOrNull(0)
        if (payResp != null) {
            visibility = VISIBLE
            ViewUtils.cleanViewVisible(this)

            mBinding.apply {
                Glide.with(context).loadImage(payResp.icon).into(ivDiamond)

                tvCoin.text = "${payResp.coin + payResp.giveCoin}"
                tvGiveCoin.text = "${payResp.giveCoin}"
                payNum.text = "$${payResp.price}"
                job?.cancel()
                job = mViewModel.viewModelScope.startCountdown(payResp.rewardsExp, 1000, {
                    val time = DateUtils.formatDuration(it).convertArabicDigitsToNormal()
                    payTime.text = time
                }) {
                    if (it == 0L) {
                        job?.cancel()
                        job = null
                        visibility = GONE
                        onTimeFinishListener?.invoke()
                    }
                }
            }
            setOnSingleClickListener {
                onSetClickListener?.invoke(payResp)
            }

        } else {
            visibility = GONE
        }
    }
}